package com.ansion.commonchat.contact.systemcontact;

import android.text.TextUtils;

import com.ansion.utils.GsonUtil;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class AndroidContactDB implements Serializable {
    /**
     *
     */
    private static final long serialVersionUID = 1685421150605871777L;

    private static final String TAG = AndroidContactDB.class.getSimpleName();

    private Map<String, AndroidContactPhoneBean> mPhoneMap = new HashMap<String, AndroidContactPhoneBean>();

    private Map<String, AndroidContactEmailBean> mEmailMap = new HashMap<String, AndroidContactEmailBean>();
    private String m_name;
    private String firstName;
    private String lastName;
    private String middleName;
    //	private Set<String> m_emails = new HashSet<String>();
    private boolean mAvatarAvailable;
    private int m_id;
    private String m_sortKey = null;

    private int nameDataVersion = -1;

    public AndroidContactDB(int id) {
        m_id = id;
    }

    public int getId() {
        return m_id;
    }

    public Set<String> getPhones() {
        return mPhoneMap.keySet();
    }

    /**
     * 获取排序后的手机号
     *
     * @return
     */
    public List<String> getPhonesSort() {
        List<AndroidContactPhoneBean> list = new ArrayList<>();
        for (AndroidContactPhoneBean bean : mPhoneMap.values()) {
            list.add(bean);
        }
        Collections.sort(list);
        List<String> phones = new ArrayList<>();
        for (AndroidContactPhoneBean bean : list) {
            phones.add(bean.getPhone());
        }
        return phones;
    }


    public void addPhoneBean(AndroidContactPhoneBean phoneBean) {
        mPhoneMap.put(phoneBean.getPhone(), phoneBean);
        phoneBean.position = mPhoneMap.size();
//        VLog.i("SyncContactsThread", "add phone for %s,phone:%s,position:%d", (firstName + lastName), phoneBean.getPhone(), phoneBean.position);
    }

    public String getOriginalPhone(String phone) {

        AndroidContactPhoneBean phoneBean = mPhoneMap.get(phone);
        if (phoneBean == null) {
            return "";
        }

        return phoneBean.getPhoneOriginal();
    }



    public Map<String, AndroidContactPhoneBean> getPhoneBeanMap() {
        return mPhoneMap;
    }

    public Collection<AndroidContactPhoneBean> getPhoneBeans() {
        return mPhoneMap.values();
    }

    public void addEmail(AndroidContactEmailBean email) {

        if (email == null || TextUtils.isEmpty(email.getAddress())) {
            return;
        }
        mEmailMap.put(email.getAddress(), email);

    }

    public Map<String, AndroidContactEmailBean> getEmails() {
        return mEmailMap;
    }

    public boolean avatarAvailable() {
        return mAvatarAvailable;
    }

    public void setAvatarAvailable(boolean available) {
        this.mAvatarAvailable = available;
    }

    public void setName(String name) {
        m_name = name;
    }

    public String getName() {
        if (!TextUtils.isEmpty(m_name)) {
            return m_name;
        }
        for (String phone : mPhoneMap.keySet()) {
            if (!TextUtils.isEmpty(phone)) {
                return phone;
            }
        }
        return m_name;
    }

    public String getSortKey() {
        return m_sortKey;
    }

    public void setSortKey(String key) {
        m_sortKey = key;
    }

    public String getFirstName() {
        if (TextUtils.isEmpty(firstName)) {
            return firstName;
        }
        for (String phone : mPhoneMap.keySet()) {
            if (firstName.equals(phone)) {
                return "";
            }
        }
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getMiddleName() {
        return middleName;
    }

    public void setMiddleName(String middleName) {
        this.middleName = middleName;
    }

    public void setNameDataVersion(int nameDataVersion) {
        this.nameDataVersion = nameDataVersion;
    }

    public int getNameDataVersion() {
        return nameDataVersion;
    }

    private static <T> String joinSetValues(Set<T> a) {
        if (a == null || a.isEmpty()) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        for (T t : a) {
            sb.append(t.toString());
            sb.append(" ");
        }
        return String.valueOf(sb);
    }

    @Override
    public String toString() {

        return GsonUtil.toJson(this);

    }

    public AndroidContactPhoneBean getPhoneBeanByPhone(String phone) {
        return mPhoneMap.get(phone);
    }

}
